<?php

/*
	Class: WatchSubscriptions

	About: Author
		Rich Joslin

	About: License
		<http://communit.as/docs/license>
*/
class WatchSubscriptions extends Cts_Db_Table_Abstract {

	/* Group: Instance Variables */

	/*
		Variable: _name
			The name of the table or view to interact with in the data source.
	*/
	protected $_name 	= 'watch_subscriptions';

	/*
		Variable: _primary
			The primary key of the table or view to interact with in the data source.
	*/
	protected $_primary = array('username', 'tablename', 'itemid');

	/* Group: Instance Methods */

	/*
		Function: getSubscriptions

		Arguments:
			$username - The username of the subscriber. Subscriptions will only be retrieved where the username column matches this argument's value.
			$order_by - An order by clause in the chosen model query language, such as SQL. Defaults to order by most recent changes first ("last_modified_on desc").

		Returns: array of feed items or an empty array
	*/
	function getSubscriptions($username, $order_by = "last_modified_on desc") {
		$where = $this->getAdapter()->quoteInto("username = ?", $username);
		$tmp_items = $this->fetchAll($where, $order_by);
		if (!is_null($tmp_items)) {
			return $tmp_items->toArray();
		} else {
			return array();
		}
	}

	/*
		Function: isSubscribed
			Indicates whether the subscription exists for a particular user.

		Arguments:
			username - The username to use in the where clause when checking if a subscription exists.
			tablename - The table name to use in the where clause when checking if a subscription exists.
			itemid - The item ID to use in the where clause when checking if a subscription exists.

		Returns: boolean
	*/
	function isSubscribed($username, $tablename, $itemid) {
		$where = $this->getAdapter()->quoteInto("username = ?", $username);
		$where .= $this->getAdapter()->quoteInto(" and tablename = ?", $tablename);
		$where .= $this->getAdapter()->quoteInto(" and itemid = ?", $itemid);
		return ($this->getCountByWhereClause($where) > 0);
	}

	/*
		Function: getWatchers

		Arguments:
			tablename - The table name of the item for which to retrieve a list of watchers.
			itemid - The ID of the item for which to retrieve a list of watchers.

		Returns: array of arrays (array of watchers) or an empty array
	*/
	function getWatchers($tablename, $itemid) {
		$where = $this->getAdapter()->quoteInto("tablename = ?", $tablename);
		$where .= $this->getAdapter()->quoteInto(" and itemid = ?", $itemid);
		$tmp_watchers = $this->fetchAll($where);
		$watchers = array();
		if (count($tmp_watchers) > 0) {
			$tmp_watchers = $tmp_watchers->toArray();
			foreach ($tmp_watchers as $tmp_watcher) {
				$watcher = array();
				$watcher['username'] = $tmp_watcher['username'];
				$watchers[] = $watcher;
			}
		}
		return $watchers;
	}

	/*
		Function: insertSubscription
			Inserts a new subscription row into the database.
			Username, tablename, and itemid make up a composite key that identifies the subscription.

		Arguments:
			username - The username of the subscribing individual.
			tablename - The name of the table assigned to the subscription.
			itemid - The ID used in the subscription that should be deleted.
			title - A title for the subscription. Should describe what is being subscribed to.
			description - A description of the content being subscribed to.
			notify - A bit/flag to determine whether to send an email after each feed item is generated. Possible values are 1 and 0.
			digest - Possible values are 'daily', 'weekly', 'monthly', 'off', and null.

		Returns: void
	*/
	function insertSubscription($username, $tablename, $itemid, $title = null, $description = null, $notify = null, $digest = null) {
		$data = array(
			'username' => $username,
			'tablename' => $tablename,
			'itemid' => $itemid,
			'title' => $title,
			'description' => $description,
			'notify' => $notify,
			'digest' => $digest,
			'created_on' => date("Y-m-d H:i:s"),
			'last_modified_on' => date("Y-m-d H:i:s")
		);
		$this->insert($data);
	}

	/*
		Function: deleteSubscription
			Deletes a subscription row from the database.
			Username, tablename, and itemid make up a composite key that identifies the subscription.
			This is not currently recursive, so all feed items will remain intact.
			Really there is no referential integrity here;
			subscriptions and feed items are treated as separate things
			(more of a cause-effect than a parent-child).

		Arguments:
			username - The username of the subscribing individual.
			tablename - The name of the database table to watch.
			itemid - The ID of the row in the database to watch.

		Returns: void
	*/
	function deleteSubscription($username, $tablename, $itemid) {
		$where = $this->getAdapter()->quoteInto('username = ?', $username);
		$where .= $this->getAdapter()->quoteInto(' and tablename = ?', $tablename);
		$where .= $this->getAdapter()->quoteInto(' and itemid = ?', $itemid);
		$this->delete($where);
	}

}
